﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using InstrumentCheck.Module;
using InstrumentCheck.BLL;
using System.Net.Mail;
using System.Collections.Generic;
using System.Text;

namespace InstrumentCheck.WebAppUI
{
    public partial class MailApproval : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Item item = new Item();
                if (item == null)
                {
                    panTable.Visible = false;
                    lblErroeMessage.Text = "對不起,出錯了,請以正常方式訪問!";
                    return;
                }
                item = getMailInfo();
                if (!CheckKeeper(item))
                {
                    panTable.Visible = false;
                    //lblErroeMessage.Text = "對不起,你無權訪問此頁面!";
                    AlertMessage("對不起,你無權訪問此頁面!", "Close");
                    return;
                }
                InitPage();
            }
        }
        protected Item  getMailInfo()
        {
            Item item = new Item();
            if (Request["fasno"].ToString() != "" && Request["Date"].ToString() != "")
            {
                item.AssetNO = Request["fasno"].ToString();
                item.NeedCorrectDate = DateTime.Parse(Request["Date"].ToString());
                item.AssetType = (AssetType)(int.Parse(Request["type"].ToString()));
                ViewState["MailInfo"] = item;
                return item;
            }
            else 
            return null;
        }
        protected bool CheckKeeper(Item  item)
        {
            User user = Session["UserInfo"] as User;
            string Keeper = CorrectQuery_bll.Keeper(item);
            return Keeper == user.EmpNO ? true : false;
        }
        protected void InitPage()
        {
            Item item = new Item();
            item = ViewState["MailInfo"] as Item;
            txtFasNO.Text = item.AssetNO;
            txtCorrectDate.Text = item.NeedCorrectDate.Value.ToShortDateString();
            txtCorrectDate.ReadOnly = true;
            txtFasNO.ReadOnly = true;
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                Item item = ViewState["MailInfo"] as Item;
                CorrectQuery_bll.UpdateAlertList(item);
                //UpdateAlertList();
                BeginSendMail(item);
                AlertMessage("確認成功!","CLOSE");
                
            }
            catch (Exception ex)
            {
                AlertMessage(ex.Message);
            }
        }
        private void BeginSendMail(Item item)
        {
            AlertMail mailInfo = new AlertMail();
            mailInfo = MailJob_bll.ApprovalMailInfo(item);
            InitMailInfo(mailInfo);
        }
     
        private void InitMailInfo(AlertMail alertmail)
        {
            int type = 0;
            switch (alertmail.Item.AssetType)
            {
                case AssetType.FixedAsset:
                    type = 1;
                    break;
                case AssetType.RestrictedItems:
                    type = 2;
                    break;
                default:
                    break;
            }
            string url = "http://localhost:1199/MailApproval.aspx?fasno=" + alertmail.Item.AssetNO.ToString() + "&date=" + alertmail.Item.NeedCorrectDate.Value.ToShortDateString() + "&type=" + type;
            string MailSubject = "儀器校驗到期通知保管人已確認 :" + alertmail.Item.AssetNO + "-" + alertmail.Item.ChiName ;
            string MailBody = "<html><body><div style=\" font-size:9pt\">主旨 = [通知] 儀器校驗到期通知，保管人已進行確認 : " + alertmail.Item.Keeper.EmpNO + " " + alertmail.Item.Keeper.EmpChineseName + "–";
            MailBody += alertmail.Item.AssetNO + "-" + alertmail.Item.ChiName + "- " + alertmail.Item.Spec + "<br>";
            MailBody += " 需完成校驗日期 = " + alertmail.Item.NeedCorrectDate.Value.ToShortDateString() + "<br> ";
            MailBody += " 通知結果 = [已完成] ”設備正在進行送校”</div>";
            List<MailAddress> MailCC = new List<MailAddress>();
            List<User> user = new List<User>();
            user = MailJob_bll.MailCC();
            foreach (User tmpUser in user)
            {
                MailAddress ads = new MailAddress(tmpUser.Email);
                MailCC.Add(ads);
            }
            //MailCC.Add(new MailAddress("YuyenTeng@aopen.com"));
            MailAddress MailTo = new MailAddress(alertmail.Item.Keeper.Email);
            sendMail(MailBody, MailSubject, MailCC.ToArray(), MailTo);

        }
        protected void sendMail(string MailBody, string MailSubject, MailAddress[] MailCC, MailAddress MailTo)
        {
            MailMessage Mail = new MailMessage();
            UTF8Encoding mailEncoding = new UTF8Encoding();
            Mail.Subject = MailSubject;
            Mail.Body = MailBody;
            if (MailCC.Length > 0)
            {
                foreach (MailAddress mailcc in MailCC)
                    Mail.CC.Add(mailcc);
            }
            Mail.To.Add(MailTo);
            Mail.From = new MailAddress("serveradmin@aopen.com");
            Mail.BodyEncoding = mailEncoding;
            Mail.IsBodyHtml = true;
            SmtpClient smtp = new SmtpClient("amzms01.aopen.com");
            smtp.Send(Mail);
        }
      
    }
}
